<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="耳标编号" prop="earNumber">
        <el-input
          v-model="queryParams.earNumber"
          placeholder="请输入耳标编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['dgw:treatment:add']"
              >新增
              </el-button>
            </el-col>-->
      <!--      <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['dgw:treatment:edit']"
              >修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['dgw:treatment:remove']"
              >删除
              </el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >申请处理
        </el-button>
      </el-col>
<!--            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                :loading="exportLoading"
                @click="handleExport"
                v-hasPermi="['dgw:treatment:export']"
              >导出
              </el-button>
            </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"  :data="treatmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="区域" align="center" prop="deptName"/>
      <el-table-column label="养殖户" align="center" prop="nickName"/>

      <el-table-column label="耳标编号" align="center" prop="earNumber"/>
      <el-table-column label="育龄" align="center" prop="age">
        <template slot-scope="scope">
          <span>{{ scope.row.age +'个月'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="phone" width="110"/>
      <el-table-column label="申请图片" align="center" prop="applicatUrl">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 60px"
            :src="scope.row.applicatUrl"
            fit="contain"
          ></el-image>
        </template>

      </el-table-column>
      <!--      <el-table-column label="用户id" align="center" prop="userId" />-->
      <el-table-column label="申请时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否处理" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==1?'danger':'primary'">
            {{ scope.row.status == 1 ? '未处理' : '已处理' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理图片" align="center" prop="disposeUrl">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 60px"
            :src="scope.row.disposeUrl"
            fit="contain"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="处理人员" align="center" prop="disposeBy"/>
      <el-table-column label="处理时间" align="center" prop="incinerationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.incinerationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="补贴金额" align="center" prop="money"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dgw:treatment:edit']"
          >处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改无危化处理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="耳标编号" prop="earNumber">
          <el-input v-model="form.earNumber" placeholder="请输入耳标编号"/>
        </el-form-item>
        <!--        <el-form-item label="焚烧时间" prop="incinerationTime">
                  <el-date-picker clearable size="small"
                    v-model="form.incinerationTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择焚烧时间">
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                  <el-input v-model="form.phone" placeholder="请输入电话" />
                </el-form-item>
                <el-form-item label="用户id" prop="userId">
                  <el-input v-model="form.userId" placeholder="请输入用户id" />
                </el-form-item>
                <el-form-item label="处理人员" prop="disposeBy">
                  <el-input v-model="form.disposeBy" placeholder="请输入处理人员" />
                </el-form-item>-->
      </el-form>

      <el-upload
        ref="upload"
        :limit="1"
        accept=".jpg, .png"
        :headers="upload.headers"
        :action="upload.url+'?tagNumber='+this.form.earNumber"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        list-type="picture"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将图片拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="耳标编号" prop="earNumber">
          <el-input v-model="form.earNumber" placeholder="请输入耳标编号"/>
        </el-form-item>
      </el-form>
      <el-upload
        ref="upload"
        :limit="1"
        :headers="upload.headers"
        :action="upload.url+'?tagNumber='+this.form.earNumber"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {
  listTreatment,
  getTreatment,
  delTreatment,
  addTreatment,
  updateTreatment,
  exportTreatment
} from '@/api/dgw/treatment'
import { getToken } from '@/utils/auth'

export default {
  name: 'Treatment',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 无危化处理表格数据
      treatmentList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        earNumber: null
      },
      // 表单参数
      form: {
        earNumber: null
      },
      // 表单校验
      rules: {},
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/dgw/treatment/importData'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询无危化处理列表 */
    getList() {
      this.loading = true
      listTreatment(this.queryParams).then(response => {
        console.log('无害化处理：', response)
        this.treatmentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        earNumber: null,
        incinerationTime: null,
        createTime: null,
        createBy: null,
        phone: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        disposeBy: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加无危化处理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTreatment(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改无危化处理'
      })
    },
    /** 提交按钮 */

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除无危化处理编号为"' + ids + '"的数据项？').then(function() {
        return delTreatment(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有无危化处理数据项？').then(() => {
        this.exportLoading = true
        return exportTreatment(queryParams)
      }).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '用户导入'
      this.upload.open = true
    },
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.upload.open = false
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTreatment(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTreatment(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
      this.$refs.upload.submit()
    }
  }
}
</script>
